package com.lvyq.javaprint.util;

import com.lvyq.javaprint.context.PrintContext;
import com.lvyq.javaprint.model.PrintData;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.OrientationRequested;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author lvyq 打印服务
 * @version 1.0
 * Create by 2024-08-07 14:11
 */
public class PrintUtils {

    //获取默认的打印机服务
    public static String defaultPrintService(){
        String printServiceName="没有找到默认打印机";
        // 获取默认的打印服务（即默认打印机）
        PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
        if (defaultPrintService != null) {
            // 打印默认打印机的名称
            System.out.println("默认打印机名称: " + defaultPrintService.getName());
            printServiceName=defaultPrintService.getName();
        } else {
            System.out.println("没有找到默认打印机。");
        }
        return printServiceName;
    }

    /**
     * @description根据source选择不同的打印服务
     * @author lvyq
     * @param[1] printContext
     * @param[2] data
     * @throws
     * @time 2024-08-06 17:22
     */
    public static void handlePrintContext(PrintContext printContext, PrintData data) throws MalformedURLException {
        checkSource(printContext,data);
    }

    /**
     * @description 校验来源类型   text 纯文本  path 本地文件地址  url 网络地址  file 文件流
     * @author lvyq
     * @throws
     * @return boolean
     * @time 2024-08-06 17:22
     */
    public static void checkSource(PrintContext printContext, PrintData data) throws MalformedURLException {
        String source = data.getSource();
        switch (source) {
            case "text": printContext.print(data.getContent(),data); return;
            case "path": printContext.print(new File(data.getContent()),data); return;
            case "url": printContext.print(new URL(data.getContent()),data); return;
            case "blob":  printContext.print(data.getFile(),data); return;
            default:
                throw new IllegalArgumentException("非法参数: " + source);
        }
    }

    /**
     * @description 打印属性设置
     * @author lvyq
     * @param[1] data
     * @throws
     * @time 2024-08-07 14:14
     */
    public static void handlePrintData(PrintData data, PrintRequestAttributeSet attributeSet){
        //打印份数
        if (data.getCopies()!=null){
            attributeSet.add(new Copies(data.getCopies()));
        }
        //纸张大小
        if (data.getPaperSize()!=null){
            handleAttributeSet(attributeSet,data.getPaperSize());
        }
        //方向
        if (data.getDirection()!=null){
            handleDirection(attributeSet,data.getDirection());
        }
    }

    /**
     * @description 方向设置
     * @author lvyq
     * @param[1] direction
     * @throws
     * @time 2024-08-07 15:00
     */
    private static void handleDirection(PrintRequestAttributeSet attributeSet,String direction) {
        switch (direction){
            case "PORTRAIT": attributeSet.add(OrientationRequested.PORTRAIT); return;
            //将要描绘的打印流页旋转 +90 度（即逆时针方向）
            case "LANDSCAPE": attributeSet.add(OrientationRequested.LANDSCAPE); return;
            //将要描绘的打印流页旋转 -90 度（即顺时针方向）
            case "REVERSE_LANDSCAPE": attributeSet.add(OrientationRequested.REVERSE_LANDSCAPE); return;
            //将要描绘的打印流页旋转 180 度
            case "REVERSE_PORTRAIT": attributeSet.add(OrientationRequested.REVERSE_PORTRAIT); return;
            default:
                throw new IllegalArgumentException(" 不支持的方向: " + direction);
        }
    }

    /**
     * @description 纸张大小
     * @author lvyq
     * @param[1] attributeSet
     * @param[2] paperSize
     * @throws

     * @time 2024-08-07 14:43
     */
    private static void handleAttributeSet(PrintRequestAttributeSet attributeSet,String paperSize) {
        switch (paperSize){
            case "A":attributeSet.add(MediaSizeName.A); return;
            case "B":attributeSet.add(MediaSizeName.B); return;
            case "C":attributeSet.add(MediaSizeName.C); return;
            case "D":attributeSet.add(MediaSizeName.D); return;
            case "E":attributeSet.add(MediaSizeName.E); return;
            case "EXECUTIVE":attributeSet.add(MediaSizeName.EXECUTIVE); return;
            case "FOLIO":attributeSet.add(MediaSizeName.FOLIO); return;
            case "INVOICE":attributeSet.add(MediaSizeName.INVOICE); return;
            case "ISO_A0":attributeSet.add(MediaSizeName.ISO_A0); return;
            case "ISO_A1":attributeSet.add(MediaSizeName.ISO_A1); return;
            case "ISO_A10":attributeSet.add(MediaSizeName.ISO_A10); return;
            case "ISO_A2":attributeSet.add(MediaSizeName.ISO_A2); return;
            case "ISO_A3":attributeSet.add(MediaSizeName.ISO_A3); return;
            case "ISO_A4":attributeSet.add(MediaSizeName.ISO_A4); return;
            case "ISO_A5":attributeSet.add(MediaSizeName.ISO_A5); return;
            case "ISO_A6":attributeSet.add(MediaSizeName.ISO_A6); return;
            case "ISO_A7":attributeSet.add(MediaSizeName.ISO_A7); return;
            case "ISO_A8":attributeSet.add(MediaSizeName.ISO_A8); return;
            case "ISO_A9":attributeSet.add(MediaSizeName.ISO_A9); return;
            default:
                throw new IllegalArgumentException("不支持的尺寸:" + paperSize);
        }
    }

    /**
     * @description 设置打印属于
     * @author lvyq
     * @param[1] data
     * @throws
     * @return PrintRequestAttributeSet
     * @time 2024-08-07 15:16
     */
    public static PrintRequestAttributeSet handleAttributeWithPrintData(PrintData data) {
        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        handlePrintData(data,attributes);
        return attributes;
    }

}
